import React, { useState, useEffect } from 'react'
import { Table, Select, Input, Button, Modal, Tooltip, Tabs } from 'antd'
import api from '../../api/activityManage.js'
import ProductModule from './ProductModule.js'
const { TabPane } = Tabs;
const SelectProductModal = (props) => {
    const [state,setState] = useState({
		// 静态交互数据
		userInfo:JSON.parse(sessionStorage.getItem('userInfo') || '{}'),
		productType:'0',// 1-自营产品；1-平台产品
		projectSelect:[],//项目类型
		selfSelectedProduct:[],//已选择的自营产品
		platformSelectedProduct:[],//已选择的平台产品
    })

    //更新state
    const updateState = (newState)=>{
        let data = JSON.parse(JSON.stringify(state))
        setState(Object.assign({},data,newState))
    }
    useEffect(()=>{
		// 获取项目类型
		api.getProjectType({},res=>{
			if(res){
				updateState({
					projectSelect:res
				})
			}
		})
    },[])
	// 添加产品
	// 自营
	const addProductSelf = (val) =>{
		updateState({selfSelectedProduct:val})
	}
	// 平台
	const addProductPlatform = (val) =>{
		updateState({platformSelectedProduct:val})
	}
	//触发搜索
	const onOk = () => {
		let arr = state.selfSelectedProduct.concat(state.platformSelectedProduct)
		props.onOk(arr)
	}
	const onCancel = () => {
		props.onCancel()
	}
    return <Modal title="添加产品" style={{height: 700}} width={950} visible={props.modalData.visible}
		wrapClassName="vertical-center-modal"
		onOk={onOk} onCancel={onCancel}>
		{/* 明世/民生机构 */}
		{state.userInfo.originOrgType==0 && <ProductModule userInfo={state.userInfo} modalData={props.modalData} projectSelect={state.projectSelect} addProductSelf={addProductSelf} addProductPlatform={addProductPlatform}></ProductModule>}
		{/* 慧简学机构 */}
	  	{state.userInfo.originOrgType==1 && <Tabs defaultActiveKey="0">
			<TabPane tab="自营产品" key="0">
				<ProductModule productType={0} userInfo={state.userInfo} modalData={props.modalData} projectSelect={state.projectSelect} addProductSelf={addProductSelf} addProductPlatform={addProductPlatform}></ProductModule>
			</TabPane>
			<TabPane tab="平台产品" key="1">
				<ProductModule productType={1} userInfo={state.userInfo} modalData={props.modalData} projectSelect={state.projectSelect} addProductSelf={addProductSelf} addProductPlatform={addProductPlatform}></ProductModule>
			</TabPane>
		</Tabs>}
    </Modal>
    
}

export default SelectProductModal